package com.na.onlineservey

import com.na.onlineservey.interview.CreateInterviewAction
import com.na.onlineservey.interview.DeleteInterviewAction
import com.na.onlineservey.interview.ListInterviewAction
import com.na.onlineservey.interview.UpdateInterviewAction
import com.na.onlineservey.interview.ReadInterviewAction
import com.na.onlineservey.interview.SearchInterviewAction

import com.docu.common.Message
import org.springframework.beans.factory.annotation.Autowired
import grails.converters.JSON

class InterviewController {

    @Autowired
    private CreateInterviewAction createInterviewAction
    @Autowired
    private UpdateInterviewAction updateInterviewAction
    @Autowired
    private ListInterviewAction listInterviewAction
    @Autowired
    private DeleteInterviewAction deleteInterviewAction
    @Autowired
    private ReadInterviewAction readInterviewAction
    @Autowired
    private SearchInterviewAction searchInterviewAction

    static allowedMethods = [create: "POST", update: "POST", delete: "POST"]

    def list = {
        List list = listInterviewAction.execute(params, null)
        render listInterviewAction.postCondition(null, list) as JSON
    }

    def show = {
        Interview interviewInstance = new Interview()
        render(template: "show", model: [interviewInstance: interviewInstance])
    }

    def interviewFirstStep = {
        render(template: "interviewFirstStep")
    }

    def genInterviewSecondStep = {
        render(template: "genInterviewSecondStep", model: [projectID: params.project.id])
    }

    def create = {
        Interview interviewInstance = new Interview(params)
        Interview interviewInstanceInstance = createInterviewAction.preCondition(null, interviewInstance)
        Message message = null
        if (interviewInstanceInstance == null) {
            message = createInterviewAction.getValidationErrorMessageForUI(interviewInstance)
        } else {
            interviewInstanceInstance = createInterviewAction.execute(null, interviewInstanceInstance)
            if (interviewInstanceInstance) {
                message = createInterviewAction.getSuccessMessageForUI(interviewInstanceInstance, createInterviewAction.SUCCESS_SAVE)
            } else {
                message = createInterviewAction.getErrorMessageForUI(interviewInstance, createInterviewAction.FAIL_SAVE)
            }
        }
        render message as JSON
    }

    def edit = {
        render readInterviewAction.execute(params, null) as JSON
    }

    def update = {
        Interview interviewInstance = new Interview(params)
        Object object = updateInterviewAction.preCondition(params, null)
        Message message = null
        if (object == false) {
            message = updateInterviewAction.getValidationErrorMessageForUI(interviewInstance)
        } else {
            int noOfRows = (int) updateInterviewAction.execute(null, object)
            if (noOfRows > 0) {
                message = updateInterviewAction.getSuccessMessageForUI(interviewInstance, updateInterviewAction.SUCCESS_UPDATE)
            } else {
                message = updateInterviewAction.getErrorMessageForUI(interviewInstance, updateInterviewAction.FAIL_UPDATE)
            }
        }
        render message as JSON
    }

    def delete = {
        Interview interviewInstance = new Interview(params)
        interviewInstance.id = Long.parseLong(params.id)
        interviewInstance.version = Long.parseLong(params.version)
        Message message = null
        if (interviewInstance) {
            int rowCount = (int) deleteInterviewAction.execute(null, interviewInstance)
            if (rowCount > 0) {
                message = deleteInterviewAction.getSuccessMessageForUI(interviewInstance, deleteInterviewAction.SUCCESS_DELETE)
            } else {
                message = deleteInterviewAction.getErrorMessageForUI(interviewInstance, deleteInterviewAction.FAIL_DELETE)
            }
        }
        else {
            message = deleteInterviewAction.getErrorMessageForUI(interviewInstance, deleteInterviewAction.ALREADY_DELETED)
        }
        render message as JSON
    }

    def search = {
        Interview interviewInstance = searchInterviewAction.execute(params.fieldName, params.fieldValue)
        if (interviewInstance) {
            render interviewInstance as JSON
        }
        else {
            render ''
        }

    }
}
